package cn.wolfcode.wolf2w.listener;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

//定义一个监听器  redis数据初始使用
//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    //监听事件ContextRefreshedEvent
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @Autowired
    private IStrategyService strategyService;

    //      spring容器启动后,初始化完成后执行该方法里的逻辑
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        System.out.println("---------------begin---------------------");
        List<Strategy> list = strategyService.list();//查出所有攻略
        for (Strategy strategy : list) {
            //如果vo对象存在就跳过不用初始化
            if (strategyStatisVORedisService.isStrategyVoExists(strategy.getId())){
                continue;
            }
            //new 出vo对象
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy,vo);//把值拷贝到vo中的对应字段 同名原则
            vo.setStrategyId(strategy.getId());//把攻略id单独设置 因为字段名对应不上
            strategyStatisVORedisService.setStrategyStatisVo(vo);//   //设置vo对象到redis
        }


        System.out.println("---------------end---------------------");


    }
}
